package com.pratices.requestAndResponse.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

public class MybatisUtils {



    public static SqlSession getSqlConnection() {

        //1.创建一个SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //2.通过Builder创建SqlSessionFactory对象
            //需要传入主配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

        //3.获取实例对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        return sqlSession;
    }
}